package com.authine.cloudpivot.ext.domain.impl;

import com.authine.cloudpivot.ext.util.MailUtis;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @program: gm-market
 * @description:
 * @author: wlw
 * @create: 2021-09-02 16:45
 **/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class MailTask implements Runnable {
    //收件人账户
    private String receiveMailAccount;
    //收件人名称
    private String receiveName;
    //商品id
    private String goodsId;
    //商品链接
    private String url;


    public final static String mailTemplate = "<p>尊敬的%s</p>\n" +
            "<p>&nbsp; &nbsp; &nbsp; &nbsp;您好，您的商品序号%s的库存数量已低于安全值，请及时维护更新，谢谢！商品链接：%s。</p>\n" +
            "<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %s</p>\n" +
            "<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %s</p>";
    private final String subject = "邮件通知";

    @Override
    public void run() {
        log.info("邮件信息:{}",this);
        try {
            if (StringUtils.isEmpty(receiveMailAccount) ||
                    StringUtils.isEmpty(receiveName) ||
                    StringUtils.isEmpty(goodsId) ||
                    StringUtils.isEmpty(url)
            ) {
                log.warn("邮件关键信息缺失,不予发送...");
                return;
            }
            String text = String.format(mailTemplate, receiveName, goodsId, url);
            MailUtis.sendEmail(receiveMailAccount, subject,text,2);
        } catch (Exception e) {
            log.error("邮件发送异常：",e);
        }
    }
}
